package org.springblade.demo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.demo.entity.Blog;
import org.springblade.demo.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/demo")
@Api(value = "演示接口", tags = "演示接口")
public class DemoController {

	@Autowired
	private BlogService blogService;

	@GetMapping("/hello")
	@ApiOperation(value = "查看信息", notes = "传入名称")
	@Cacheable(cacheNames = "demo-info", key = "#name")
	public R<String> hello(String name) {
		log.info("本条信息没有从缓存获取");
		return R.data("Hello, My Name Is: " + name);
	}

	@GetMapping("/remove_hello")
	@CacheEvict(cacheNames = "demo-info", key = "#name")
	public R<String> removeHello(String name) {
		return R.success("删除成功");
	}

	@PostMapping("/add")
	public R add(@RequestBody Blog blog) {
		return R.status(blogService.save(blog));
	}

	@PutMapping("/update")
	public R update(@RequestBody Blog blog) {
		return R.status(blogService.updateById(blog));
	}
	@DeleteMapping("/del/{id}")
	public R del(@PathVariable Long id) {
		return R.status(blogService.removeById(id));
	}

	@GetMapping("/getBlog")
	public R<List<Blog>> getBlog() {
		return R.data(blogService.list());
	}

	@GetMapping("/Page")
	public R<IPage<Blog>> Page(@ApiIgnore @RequestParam Map<String, Object> blog, Query query) {
	  return R.data(blogService.page(Condition.getPage(query), Condition.getQueryWrapper(blog, Blog.class)))	;
	}

	@GetMapping("/detail")
	public R<Blog> detail(Long id) {
		Blog detail = blogService.getById(id);
		return R.data(detail);
	}


}
